home *** CD-ROM | disk | FTP | other *** search
/ IRIX 6.5 Applications 1999 May / SGI IRIX 6.5 Applications 1999 May.iso / dist / insight.idb / usr / share / Insight / xhelp / samples / exampleApp / exampleAppXm.c.z / exampleAppXm.c
C/C++ Source or Header  |  1998-05-04  |  13KB  |  480 lines

  1. /*_____________________________________________________________________________
  2.  *
  3.  * File:         exampleAppXm.c
  4.  *
  5.  * Date:         3/25/94
  6.  *
  7.  * Compile with: cc -o exampleAppXm exampleAppXm.c -lhelpmsg -lXm -lXt -lX11
  8.  *
  9.  * Purpose:      An simple example program that shows how to use the SGI
  10.  *               Help system from a Motif application.
  11.  *
  12.  *               This program displays a few buttons on a bulletin board 
  13.  *               alongwith a help menu. The use of context sensitive help
  14.  *               is also demonstrated.
  15.  *_____________________________________________________________________________
  16.  */
  17.  
  18. /*
  19.  * standard include files
  20.  */
  21.  
  22. #include <stdio.h>
  23. #include <unistd.h>
  24. #include <fcntl.h>
  25. #include <X11/cursorfont.h>
  26. #include <Xm/Xm.h>
  27. #include <Xm/Label.h>
  28. #include <Xm/PushB.h>
  29. #include <Xm/Form.h>
  30. #include <Xm/MessageB.h>
  31. #include <Xm/MainW.h>
  32. #include <Xm/RowColumn.h>
  33. #include <Xm/CascadeB.h>
  34. #include <Xm/Separator.h>
  35.  
  36. /*
  37.  * include for for calling/using SGIHelp
  38.  */
  39.  
  40. #include <helpapi/HelpBroker.h>
  41.  
  42.  
  43. /*
  44.  * forward declarations of functions
  45.  */
  46.  
  47. Widget initMotif(int          *argcP,            /* Initializes motif and    */
  48.          char         *argv[],           /* and returns the top level*/
  49.          XtAppContext *app_contextP,     /* shell.                   */
  50.          Display      **displayP);              
  51.  
  52. void createInterface(Widget parent);             /*creates the main window,  */
  53.                                                  /*menus, and the buttons    */
  54.                                                  /*on the main window        */
  55.  
  56. void clickForHelpCB();                           /*callbacks for each of     */
  57. void overviewCB();                               /*the help menu's           */
  58. void taskCB();
  59. void indexCB();
  60. void keysAndShortcutsCB();
  61. void productInfoCB();
  62.  
  63. void infoDialogCB();
  64.  
  65.  
  66. Widget _mainWindow, _infoDialog=NULL;
  67.  
  68.  
  69.  
  70. /*_____________________________________________________________________________
  71.  *
  72.  * main()
  73.  *_____________________________________________________________________________
  74.  */
  75.  
  76. main(int argc, char *argv[])
  77. {
  78.     Display      *display;
  79.     XtAppContext app_context;
  80.     Widget       toplevel;
  81.     
  82.     toplevel = initMotif (&argc,argv,&app_context,&display);
  83.  
  84.     createInterface(toplevel);
  85.  
  86.     XtRealizeWidget(toplevel);
  87.  
  88.     XtAppMainLoop(app_context);
  89. }
  90.  
  91. /*_____________________________________________________________________________
  92.  *
  93.  * Function: initMotif()
  94.  *
  95.  * Purpose:  Initializes Motif and creates a top level shell.
  96.  *           Returns the toplevel shell.
  97.  *
  98.  *           Makes the call to initialize variables for the SGIHelp
  99.  *           interface...note that it does not *start* the sgihelp
  100.  *           process. That is done when a request for help is made,
  101.  *           if and only if the sgihelp process is not already
  102.  *           running.
  103.  *
  104.  *_____________________________________________________________________________
  105.  */
  106. Widget initMotif(int *argcP,char *argv[],XtAppContext *app_contextP,
  107.          Display **displayP)
  108. {
  109.     Widget toplevel;
  110.  
  111.     XtToolkitInitialize();
  112.     *app_contextP = XtCreateApplicationContext();
  113.     *displayP     = XtOpenDisplay(*app_contextP,NULL,"exampleAppXm",
  114.                   "exampleAppXmClass",NULL,
  115.                   0,argcP, argv);
  116.  
  117.     if (*displayP == NULL) {
  118.     fprintf (stderr,"Could not open display.\n");
  119.     fprintf (stderr,"Check your DISPLAY environment variable.\n");
  120.     fprintf (stderr,"Exiting...\n");
  121.     exit(-1);
  122.     }
  123.     
  124.     toplevel = XtAppCreateShell("exampleAppXm", NULL, 
  125.                                 applicationShellWidgetClass,
  126.                 *displayP, NULL,0);
  127.     
  128. /* 
  129.  * initialize variables for SGIHelp 
  130.  */
  131.     SGIHelpInit(*displayP, "exampleAppXm", ".");
  132.  
  133.     return (toplevel);   
  134. }
  135.  
  136.  
  137. /*_____________________________________________________________________________
  138.  *
  139.  * Function:   createInterface()
  140.  *_____________________________________________________________________________
  141.  */
  142. void createInterface(Widget parent)
  143. {
  144.     Arg    args[10];
  145.     int    i;
  146.     Widget baseForm;
  147.     Widget menuBar;
  148.     Widget demoLabel, demoButton;
  149.     Widget pulldown1,pulldown2, cascade1, cascade2;
  150.     Widget menuButtons[6];   /*we will create at max 6 buttons on a menu*/
  151.     XmString xmStr;
  152.  
  153. /*
  154.  * mainWindow is an XmMainWindow
  155.  * on which the whole interface is built
  156.  */
  157.     i=0;
  158.     _mainWindow = XmCreateMainWindow(parent,"mainWindow",args,i);
  159.     XtManageChild(_mainWindow);
  160.  
  161. /*
  162.  * baseForm is the workArea for the
  163.  * mainWindow above.
  164.  */
  165.     i=0;
  166.     XtSetArg (args[i],XmNwidth,400);i++;
  167.     XtSetArg (args[i],XmNheight,300);i++;
  168.     XtSetArg (args[i],XmNverticalSpacing,40);i++;
  169.     baseForm = XmCreateForm(_mainWindow,"baseForm",args,i);
  170.     XtManageChild(baseForm);
  171.  
  172. /*
  173.  * On this bulletin board, put a label and a button
  174.  * for demonstrating callbacks and context sensitive
  175.  * help.
  176.  */
  177.     i=0;
  178.     xmStr = XmStringCreateSimple("SGI Help!");
  179.     XtSetArg (args[i],XmNlabelString,xmStr);i++;
  180.     XtSetArg (args[i],XmNtopAttachment,XmATTACH_FORM);i++;
  181.     XtSetArg (args[i],XmNrightAttachment,XmATTACH_FORM);i++;
  182.     XtSetArg (args[i],XmNleftAttachment,XmATTACH_FORM);i++;
  183.     XtSetArg (args[i],XmNalignment,XmALIGNMENT_CENTER);i++;
  184.     demoLabel = XmCreateLabel(baseForm,"sgiHelpLabel",args,i);
  185.     XtManageChild(demoLabel);
  186.     XmStringFree(xmStr);
  187.     
  188.     i=0;
  189.     xmStr = XmStringCreateSimple("Click Here For Help");
  190.     XtSetArg (args[i],XmNlabelString,xmStr);i++;
  191.     XtSetArg (args[i],XmNrightAttachment,XmATTACH_FORM);i++;
  192.     XtSetArg (args[i],XmNbottomAttachment,XmATTACH_FORM);i++;
  193.     demoButton = XmCreatePushButton(baseForm,"sgiHelpPushButton",args,i);
  194.     XtManageChild(demoButton);
  195.     XmStringFree(xmStr);
  196.     XtAddCallback(demoButton,XmNactivateCallback,taskCB,NULL);
  197.  
  198. /*
  199.  * build a pulldown menu system, including the "help" menu
  200.  */
  201.     menuBar   = XmCreateMenuBar(_mainWindow,"menuBar",NULL,0);
  202.     XtManageChild(menuBar);
  203.  
  204.     pulldown1 = XmCreatePulldownMenu(menuBar,"pulldown1",NULL,0);
  205.     pulldown2 = XmCreatePulldownMenu(menuBar,"pulldown2",NULL,0);
  206.  
  207.     i=0;
  208.     XtSetArg (args[i],XmNsubMenuId,pulldown1);i++;
  209.     cascade1 = XmCreateCascadeButton(menuBar,"File",args,i);
  210.     XtManageChild(cascade1);
  211.  
  212.     i=0;
  213.     XtSetArg (args[i],XmNsubMenuId,pulldown2);i++;
  214.     cascade2 = XmCreateCascadeButton(menuBar,"Help",args,i);
  215.     XtManageChild(cascade2);
  216.  
  217. /*
  218.  * Declare this to be the Help menu
  219.  */
  220.     i=0;
  221.     XtSetArg (args[i],XmNmenuHelpWidget,cascade2);i++;
  222.     XtSetValues(menuBar,args,i);
  223.  
  224.     menuButtons[0] = XmCreatePushButton(pulldown1,"Exit",NULL,0);
  225.     XtManageChildren(menuButtons,1);
  226.     XtAddCallback(menuButtons[0],XmNactivateCallback,(XtCallbackProc)exit,0);
  227.  
  228.     menuButtons[0] = XmCreatePushButton(pulldown2,"Click for Help",NULL,0);
  229.     menuButtons[1] = XmCreatePushButton(pulldown2,"Overview",NULL,0);
  230.     XtManageChild( XmCreateSeparator(pulldown2, "separator1",NULL,0) );
  231.     menuButtons[2] = XmCreatePushButton(pulldown2,"Sample Help Task",NULL,0);
  232.     XtManageChild( XmCreateSeparator(pulldown2, "separator2",NULL,0) );
  233.     menuButtons[3] = XmCreatePushButton(pulldown2,"Index",NULL,0);
  234.     menuButtons[4] = XmCreatePushButton(pulldown2,"Keys and Shortcuts",NULL,0);
  235.     XtManageChild( XmCreateSeparator(pulldown2, "separator3",NULL,0) );
  236.     menuButtons[5] = XmCreatePushButton(pulldown2,"Product Information",NULL,0);
  237.  
  238.     XtManageChildren(menuButtons,6);
  239.  
  240. /*
  241.  * add callbacks to each of the help menu buttons
  242.  */
  243.     XtAddCallback(menuButtons[0],XmNactivateCallback,clickForHelpCB,NULL);
  244.     XtAddCallback(menuButtons[1],XmNactivateCallback,overviewCB,NULL);
  245.     XtAddCallback(menuButtons[2],XmNactivateCallback,taskCB,NULL);
  246.     XtAddCallback(menuButtons[3],XmNactivateCallback,indexCB,NULL);
  247.     XtAddCallback(menuButtons[4],XmNactivateCallback,keysAndShortcutsCB,NULL);
  248.     XtAddCallback(menuButtons[5],XmNactivateCallback,productInfoCB,NULL);
  249.  
  250. /*
  251.  * set the bulletin board and menubar into
  252.  * the main Window.
  253.  */
  254.     XmMainWindowSetAreas(_mainWindow,menuBar,NULL,NULL,NULL,baseForm);
  255. }
  256.  
  257.  
  258. /*_____________________________________________________________________________
  259.  *
  260.  * void clickForHelpCB()
  261.  *
  262.  * Purpose:  Provides context-sensitivity within an application;
  263.  *           makes a request to the sgihelp process.
  264.  *
  265.  *_____________________________________________________________________________
  266.  */
  267.  
  268. void clickForHelpCB(Widget wid, XtPointer clientData, XtPointer callData)
  269. {
  270.      static Cursor cursor = NULL;
  271.      static char path[512], tmp[512];
  272.      Widget shell, result, w;
  273.   
  274.      strcpy(path, "");
  275.      strcpy(tmp,  "");
  276.  
  277. /* 
  278.  * create a question-mark cursor 
  279.  */
  280.      if(!cursor)
  281.          cursor = XCreateFontCursor(XtDisplay(wid), XC_question_arrow);
  282.  
  283.      XmUpdateDisplay(_mainWindow);
  284.  
  285. /* 
  286.  * get the top-level shell for the window 
  287.  */
  288.      shell = _mainWindow;
  289.      while (shell && !XtIsShell(shell)) {
  290.             shell = XtParent(shell);
  291.      }
  292.  
  293. /*
  294.  * modal interface for selection of a component;
  295.  * returns the widget or gadget that contains the pointer
  296.  */
  297.      result = XmTrackingLocate(shell, cursor, FALSE);
  298.  
  299.      if( result ) {
  300.          w = result;
  301.  
  302. /* 
  303.  * get the widget hierarchy; separate with a '.';
  304.  * this also puts them in top-down vs. bottom-up order.
  305.  */
  306.          do {
  307.               if( XtName(w) ) {
  308.                   strcpy(path, XtName(w));
  309.  
  310.                   if( strlen(tmp) > 0 ) {
  311.                       strcat(path, ".");
  312.                       strcat(path, tmp);
  313.                   }
  314.  
  315.                   strcpy(tmp, path);
  316.               }
  317.  
  318.               w = XtParent(w);
  319.          } while (w != NULL && w != shell);
  320.  
  321.          /*
  322.           * send msg to the help server-widget hierarchy;
  323.           *      OR
  324.           * provide a mapping to produce the key to be used
  325.           *
  326.       * In this case, we'll let the sgihelp process do
  327.       * the mapping for us, with the use of a helpmap file
  328.           *
  329.           * Note that parameter 2, the book name, can be found
  330.           * from the helpmap file as well. The developer need
  331.       * not hard-code it, if a helpmap file is present for
  332.       * the application. 
  333.           *
  334.           */
  335.           if( strlen(path) > 0 ) {
  336.         SGIHelpMsg(path, NULL, NULL);
  337.           }
  338.      }
  339. }
  340.  
  341.  
  342. /*_____________________________________________________________________________
  343.  *
  344.  * void overviewCB()
  345.  *_____________________________________________________________________________
  346.  */
  347. void overviewCB()
  348. {
  349.  
  350. /*
  351.  * Using the mapping file allows us to specify
  352.  * a "Overview" help card for each window in
  353.  * our application. In this case, we will point
  354.  * to a specific one. Note that the book name is
  355.  * specified, but not necessary if a helpmap file
  356.  * exists for this application.
  357.  */
  358.     SGIHelpMsg("overview", "exampleAppXmHelp", NULL);
  359. }
  360.  
  361.  
  362. /*_____________________________________________________________________________
  363.  *
  364.  * void indexCB()
  365.  *_____________________________________________________________________________
  366.  */
  367. void indexCB()
  368. {
  369.  
  370. /*
  371.  * For the index window to work for this application,
  372.  * a helpmap file MUST be present!
  373.  */
  374.     SGIHelpIndexMsg("index", NULL);
  375. }
  376.  
  377.  
  378. /*_____________________________________________________________________________
  379.  *
  380.  * void taskCB()
  381.  *_____________________________________________________________________________
  382.  */
  383. void taskCB()
  384. {
  385.  
  386. /*
  387.  * For the task found in the help menu or a pushbutton, we
  388.  * use a specific key/book combination.
  389.  */
  390.     SGIHelpMsg("help_task", "exampleAppXmHelp", NULL);
  391. }
  392.  
  393.  
  394. /*_____________________________________________________________________________
  395.  *
  396.  * void keysAndShortcutsCB()
  397.  *_____________________________________________________________________________
  398.  */
  399. void keysAndShortcutsCB()
  400. {
  401.  
  402. /*
  403.  * This would point to the help card that contains
  404.  * information about the use of keys/accelerators, etc.
  405.  * for your application.
  406.  */
  407.     SGIHelpMsg("keys", "exampleAppXmHelp", NULL);
  408. }
  409.  
  410.  
  411. /*_____________________________________________________________________________
  412.  *
  413.  * void productInfoCB()
  414.  *_____________________________________________________________________________
  415.  */
  416. void productInfoCB()
  417. {
  418.  
  419. /*
  420.  * Pops up a dialog showing product version information.
  421.  *
  422.  * This area has nothing to do with SGIHelp, but is included
  423.  * for completeness.
  424.  */
  425.  
  426.   void     buildInfoDialog();
  427.  
  428.   XmString xmStr;
  429.   Arg      args[10];
  430.   int      i;
  431.  
  432.     if( _infoDialog == NULL ) {
  433.         buildInfoDialog();
  434.         XtRealizeWidget( _infoDialog );
  435.     }
  436.  
  437.     xmStr=XmStringCreateSimple("Example Motif App Using SGIHelp version 1.0");
  438.     i=0;
  439.     XtSetArg (args[i],XmNmessageString,xmStr);i++;
  440.     XtSetValues(_infoDialog, args, i);
  441.     XmStringFree(xmStr);
  442.  
  443.     XtManageChild(_infoDialog);
  444. }
  445.  
  446.  
  447. void buildInfoDialog()
  448. {
  449.   Arg    args[10];
  450.   int    i;
  451.  
  452. /*
  453.  * Build the informational dialog to display the version info
  454.  */
  455.     i=0;
  456.     XtSetArg (args[i],XmNautoUnmanage,True);i++;
  457.     XtSetArg (args[i],XmNdialogType,XmDIALOG_WORKING);i++;
  458.     XtSetArg (args[i],XmNdialogStyle,XmDIALOG_APPLICATION_MODAL);i++;
  459.     _infoDialog = XmCreateInformationDialog(_mainWindow,"infoDialog",args,i);
  460.  
  461.     XtAddCallback(_infoDialog, XmNokCallback, infoDialogCB, NULL);
  462.  
  463.     XtUnmanageChild(XmMessageBoxGetChild(_infoDialog, XmDIALOG_CANCEL_BUTTON));
  464.     XtUnmanageChild(XmMessageBoxGetChild(_infoDialog, XmDIALOG_HELP_BUTTON));
  465. }
  466.  
  467.  
  468. void infoDialogCB()
  469. {
  470.     if ( _infoDialog ) {
  471.          XtUnmanageChild(_infoDialog);
  472.  
  473.          /* Explicitly set the input focus */
  474.          XSetInputFocus(XtDisplay(_mainWindow), PointerRoot, 
  475.                         RevertToParent, CurrentTime);
  476.     }
  477. }
  478.  
  479.  
  480.